package com.gopay.bis.service.bank.task;


import java.util.HashMap;
import java.util.Map;

import javax.annotation.Resource;
import javax.naming.CommunicationException;

import com.gopay.remote.cashier.bis.CashierBgNotifyRemoteService;
import org.apache.commons.lang.builder.ToStringBuilder;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.gopay.common.Result;
import com.gopay.common.domain.bank.MtPayBaseMessage;
import com.gopay.common.util.HttpTransport;
import com.gopay.remote.bank.MtPayInteractRemoteService;

public class MtPayTaskThread implements Runnable {
	
	private static Logger logger = LoggerFactory.getLogger(MtPayTaskThread.class);
	
	private MtPayBaseMessage mtPayMsg;

	private MtPayInteractRemoteService mtPaybankInteractService;
	
	private HttpTransport notifyCashier;

	private CashierBgNotifyRemoteService cashierBgNotifyRemoteService;
	
	public MtPayTaskThread(MtPayBaseMessage mtPayMsg,MtPayInteractRemoteService mtPaybankInteractService,CashierBgNotifyRemoteService cashierBgNotifyRemoteService){
		this.mtPaybankInteractService = mtPaybankInteractService;
		this.mtPayMsg = mtPayMsg;
		this.cashierBgNotifyRemoteService = cashierBgNotifyRemoteService;
	}
	
	public void run() {
		logger.error("MtPayTaskThread process order:",ToStringBuilder.reflectionToString(mtPayMsg));
		Result result = mtPaybankInteractService.pay(mtPayMsg);
		if (!result.isSuccess()) {// 处理失败
			logger.error("MtPayTaskThread Exception", result.getResultCode());
		}

		// 测试银行报文加密并组装
		if (mtPayMsg.getBank().equals("TBANK")) {
			Map<String, String> param = new HashMap<String, String>();
			// 验签成功入账
			param.put("orderId", mtPayMsg.getOrderId());
			param.put("amt", mtPayMsg.getAmount().toString());
			param.put("cardType", "Unknown");
			param.put("notifyNum", "1");
			param.put("payStatus", "00");
			param.put("retCode", "00");
			param.put("retMsg", "验签成功");
			param.put("signData", "");
			param.put("isFront", "false");
			try {
				/*notifyCashier.submit(param);*/ //生产优化10期 lx 2017-9-30 16:23:37
				cashierBgNotifyRemoteService.callBgNotify(param);
			} catch (Exception e) {
				logger.error("后台通知cashier调用异常：",e);
				//e.printStackTrace();
			}
		}
	}

	public MtPayBaseMessage getMtPayMsg() {
		return mtPayMsg;
	}

	public void setMtPayMsg(MtPayBaseMessage mtPayMsg) {
		this.mtPayMsg = mtPayMsg;
	}
	
	public HttpTransport getNotifyCashier() {
		return notifyCashier;
	}

	public void setNotifyCashier(HttpTransport notifyCashier) {
		this.notifyCashier = notifyCashier;
	}

	public CashierBgNotifyRemoteService getCashierBgNotifyRemoteService() {
		return cashierBgNotifyRemoteService;
	}

	public void setCashierBgNotifyRemoteService(CashierBgNotifyRemoteService cashierBgNotifyRemoteService) {
		this.cashierBgNotifyRemoteService = cashierBgNotifyRemoteService;
	}
}
